#import "ResultsViewController.h"
#import "UAProgressView.h"
#import "Player.h"
#import "Reactor.h"
#import "IntroViewController.h"


@interface ResultsViewController ()
@property (weak, nonatomic) IBOutlet UILabel *playerOneTotal;
@property (weak, nonatomic) IBOutlet UILabel *playerTwoTotal;
@property (weak, nonatomic) IBOutlet UIView *playerTwoView;
@property (weak, nonatomic) IBOutlet UILabel *playerOneObservation;
@property (weak, nonatomic) IBOutlet UILabel *playerOneKnowedge;
@property (weak, nonatomic) IBOutlet UILabel *playerOneReaction;
@property (weak, nonatomic) IBOutlet UILabel *playerTwoObservation;
@property (weak, nonatomic) IBOutlet UILabel *playerTwoKnowedge;
@property (weak, nonatomic) IBOutlet UILabel *playerTwoReaction;

@property (weak, nonatomic) IBOutlet UIView *playerOneOverall;
@property (weak, nonatomic) IBOutlet UIView *playerTwoOverall;

@property (weak, nonatomic) IBOutlet UILabel *playerOneName;
@property (weak, nonatomic) IBOutlet UILabel *playerTwoName;

@property UAProgressView *playerOneResult;
@property UAProgressView *playerTwoResult;

@property Player *playerOne;
@property Player *playerTwo;
@property Reactor *reactor;

@property CGFloat playerOnePercentage;
@property CGFloat playerTwoPercentage;


@end

@implementation ResultsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //timer to fill circle progress bars
    [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(updateProgress:) userInfo:nil repeats:NO];
    
    //rotate playerTwo side of the screen
    self.playerTwoView.transform = CGAffineTransformMakeRotation(M_PI);
   
    //get info for players and scores
    self.reactor = [Reactor sharedReactor];
    self.playerOne = self.reactor.playerOne;
    self.playerTwo = self.reactor.playerTwo;
    
    if  ((self.playerOne.efficiency + self.playerTwo.efficiency)==0) {
        self.playerOnePercentage = 0;
        self.playerTwoPercentage = 0;
    }
    else {
    self.playerOnePercentage = (self.playerOne.efficiency* 100) / (self.playerOne.efficiency + self.playerOne.unefficiency);
    self.playerTwoPercentage = (self.playerTwo.efficiency * 100) / (self.playerTwo.efficiency + self.playerTwo.unefficiency);
    }
    
    self.playerOneResult = [[UAProgressView alloc] initWithFrame:CGRectMake(50, 70, 160, 160)];
    self.playerTwoResult = [[UAProgressView alloc] initWithFrame:CGRectMake(50, 70, 160, 160)];
    
    //fill visual components with actual data
    [self.playerOneOverall addSubview:self.playerOneResult];
    [self.playerTwoOverall addSubview:self.playerTwoResult];
    
    [self setupPlayerProgressView:self.playerOneResult];
    [self setupPlayerProgressView:self.playerTwoResult];
    
    self.playerOneName.text = self.playerOne.name;
    self.playerTwoName.text = self.playerTwo.name;
    
    self.playerOneTotal.text = [NSString stringWithFormat:@"%d",(self.playerOne.reactionScore + self.playerOne.knowledgeScore + self.playerOne.observationScore)];
    self.playerTwoTotal.text = [NSString stringWithFormat:@"%d",(self.playerTwo.reactionScore + self.playerTwo.knowledgeScore + self.playerTwo.observationScore)];
    
    self.playerOneReaction.text = [NSString stringWithFormat:@"%ld", (long)self.playerOne.reactionScore];
    self.playerOneKnowedge.text = [NSString stringWithFormat:@"%ld", (long)self.playerOne.knowledgeScore];
    self.playerOneObservation.text = [NSString stringWithFormat:@"%ld", (long)self.playerOne.observationScore];
    
    self.playerTwoReaction.text = [NSString stringWithFormat:@"%ld", (long)self.playerTwo.reactionScore];
    self.playerTwoKnowedge.text = [NSString stringWithFormat:@"%ld", (long)self.playerTwo.knowledgeScore];
    self.playerTwoObservation.text = [NSString stringWithFormat:@"%ld", (long)self.playerTwo.observationScore];
    
}
//new game
- (IBAction)restartGame:(id)sender {
    IntroViewController *roundsViewController = [[IntroViewController alloc] init];
    [self.navigationController pushViewController:roundsViewController animated:YES];
}
#pragma mark Circle Progress Bar
- (void)setupPlayerProgressView:(UAProgressView*) view {
    
    view.tintColor = [UIColor colorWithRed:0 green:1 blue:0 alpha:0.75];
    view.borderWidth = 7.0;
    view.lineWidth = 12.0;
    
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 130, 130)];
    [label setTextAlignment:NSTextAlignmentCenter];
    label.userInteractionEnabled = NO;
    
    label.numberOfLines = 0;
    label.textColor = [UIColor whiteColor];
    label.font = [UIFont systemFontOfSize:30];
    view.centralView = label;
    view.fillOnTouch = NO;
    if ([view isEqual:self.playerOneResult])
        label.text = [NSString stringWithFormat:@"%.1f%%", self.playerOnePercentage];
    else
        label.text = [NSString stringWithFormat:@"%.1f%%", self.playerTwoPercentage];

}

- (void)updateProgress:(NSTimer *)timer {
    [self.playerOneResult setProgress:self.playerOnePercentage/100 animated:YES];
    [self.playerTwoResult setProgress:self.playerTwoPercentage/100 animated:YES];

}


@end
